package com.tianye.lc.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.avos.avoscloud.AVObject;
import com.tianye.lc.R;
import com.tianye.lc.activity.BaseActivity;
import com.tianye.lc.activity.YuhuaguRoseDetailActivity;
import com.tianye.lc.activity.YuhuaguRoseListActivity;
import com.tianye.lc.common.Final;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangyazhou on 2016/12/20.
 */

public class YuhuaguListRecyclerAdapter extends RecyclerView.Adapter<YuhuaguListRecyclerAdapter.ListViewHolder> {
    private BaseActivity mActivity;

    private List<AVObject> originalBotanyList;
    private List<AVObject> botanyList;

    public YuhuaguListRecyclerAdapter(BaseActivity activity) {
        mActivity = activity;
    }

    public void setData(List<AVObject> botanyList) {
        originalBotanyList = botanyList;
        this.botanyList = botanyList;
        notifyDataSetChanged();
    }

    public void filter(String text,boolean isName) {
        if (TextUtils.isEmpty(text)) {
            setData(originalBotanyList);
            return;
        }
        List<AVObject> tempList = new ArrayList<>();
        for (int i = 0; i < originalBotanyList.size(); i++) {
            AVObject avObject = originalBotanyList.get(i);
            if (isName) {
                String name = "";
                String name1 = avObject.getString("3E7AF3283E90A088DED2D273A0A6ACA4");
                String name2 = avObject.getString("1E6AC400A1BF4E35D14C260CA90BADFF");
                String name3 = avObject.getString("AD72A53DA5C042207C5D55E9E7DF2E82");
                if (!TextUtils.isEmpty(name1)) {
                    name = name1;
                } else if (!TextUtils.isEmpty(name2)) {
                    name = name2;
                } else if (!TextUtils.isEmpty(name3)) {
                    name = name3;
                }
                if (name.contains(text)) {
                    tempList.add(avObject);
                }
            }else {
                if (avObject.toString().contains(text)) {
                    tempList.add(avObject);
                }
            }
        }
        this.botanyList = tempList;
        notifyDataSetChanged();
    }

    public void updateListLike(String roseId, boolean like) {
        for (int i = 0; i < botanyList.size(); i++) {
            AVObject avObject = botanyList.get(i);
            if (avObject.getObjectId().equals(roseId)) {
                avObject.put("like", like);
                return;
            }
        }
    }

    @Override
    public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ListViewHolder(mActivity.getLayoutInflater().inflate(R.layout.list_item_breed, parent, false));
    }

    @Override
    public void onBindViewHolder(ListViewHolder holder, int position) {
        AVObject object = botanyList.get(position);
        holder.id.setText(position + ":" + object.getObjectId());
        String name = "";
        String name1 = object.getString("3E7AF3283E90A088DED2D273A0A6ACA4");
        String name2 = object.getString("1E6AC400A1BF4E35D14C260CA90BADFF");
        String name3 = object.getString("AD72A53DA5C042207C5D55E9E7DF2E82");
        if (!TextUtils.isEmpty(name1)) {
            name = name1;
        } else if (!TextUtils.isEmpty(name2)) {
            name = name2;
        } else if (!TextUtils.isEmpty(name3)) {
            name = name3;
        }
        holder.name.setText(name);

        try {
            if (object.getBoolean("like")) {
                holder.like.setVisibility(View.VISIBLE);
            } else {
                holder.like.setVisibility(View.GONE);
            }
        } catch (Exception e) {
            holder.like.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {
        if (null == botanyList) {
            return 0;
        }
        return botanyList.size();
    }

    public class ListViewHolder extends RecyclerView.ViewHolder {
        private TextView name, id;
        private View like;

        public ListViewHolder(View itemView) {
            super(itemView);
            id = (TextView) itemView.findViewById(R.id.id);
            name = (TextView) itemView.findViewById(R.id.name);
            like = itemView.findViewById(R.id.like);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    Intent intent = new Intent(mActivity, YuhuaguRoseDetailActivity.class);
                    intent.putExtra(Final.ROSE_INFO, botanyList.get(position).toJSONObject().toString());
                    intent.putExtra(Final.ROSE_NAME, name.getText().toString());
                    mActivity.startActivityForResult(intent, YuhuaguRoseListActivity.REQUEST_ROSE_DETAIL);
                }
            });
        }
    }

}
